package 高效制胜._09;

import java.util.Collections;
import java.util.List;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/9/17 16:41
 * 通过删除字母匹配到字典里最长单词
 * 算法：双指针  排序
 */
public class _524 {

    public String findLongestWord(String s, List<String> dictionary) {
        Collections.sort(dictionary, (a, b) -> {
            if (a.length() != b.length()) return b.length() - a.length();
            return a.compareToIgnoreCase(b);
        });
        int len = s.length();
        for (String dic : dictionary) {
            int p = 0, m = dic.length();
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) == dic.charAt(p) && ++p == m) return dic;
            }
        }
        return "";
    }

}
